package com.kgc.warehouse.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.warehouse.system.common.CodeMsg;
import com.kgc.warehouse.system.common.ResponseResult;
import com.kgc.warehouse.system.dto.RolePageDto;
import com.kgc.warehouse.system.entity.Role;
import com.kgc.warehouse.system.entity.RolePermission;
import com.kgc.warehouse.system.entity.UserRole;
import com.kgc.warehouse.system.mapper.RoleMapper;
import com.kgc.warehouse.system.mapper.RolePermissionMapper;
import com.kgc.warehouse.system.mapper.UserRoleMapper;
import com.kgc.warehouse.system.service.RoleService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
* @author main
* @description 针对表【sys_role】的数据库操作Service实现
* @createDate 2023-02-03 20:07:42
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{

    @Autowired(required = false)
    private RoleMapper roleMapper;

    @Autowired(required = false)
    private UserRoleMapper userRoleMapper;
    @Autowired(required = false)
    private RolePermissionMapper rolePermissionMapper;
    @Override
    public ResponseResult selectPage(RolePageDto rolePageDto) {

        Page<Role> rolePage = new Page<>(rolePageDto.getPage(),rolePageDto.getLimit());
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleLambdaQueryWrapper.like(!StringUtils.isNullOrEmpty(rolePageDto.getRoleName()),Role::getName,rolePageDto.getRoleName());
        roleMapper.selectPage(rolePage,roleLambdaQueryWrapper);
        List<Role> records = rolePage.getRecords();
        long total = rolePage.getTotal();

        return new ResponseResult(records,total, CodeMsg.SUCCESS);
    }

    @Override
    public ResponseResult add(Role role) {

        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleLambdaQueryWrapper.eq(Role::getName,role.getName());
        Role role1 = roleMapper.selectOne(roleLambdaQueryWrapper);
        if (ObjectUtils.isNotEmpty(role1))
        {
            return new ResponseResult(CodeMsg.ROLE_EXIST);
        }
        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        df.format(day);
        role.setCreatetime(day);
        roleMapper.insert(role);

        return new ResponseResult(CodeMsg.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(String ids) {

        String[] split = ids.split(",");
        ArrayList<Integer> integers = new ArrayList<>();
        for (String s : split) {
            integers.add(Integer.valueOf(s));
        }
        //查询所删除的角色是否被占用
        LambdaQueryWrapper<UserRole> userRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userRoleLambdaQueryWrapper.in(UserRole::getRid,integers);
        List<UserRole> userRoles = userRoleMapper.selectList(userRoleLambdaQueryWrapper);
        if (userRoles.size()>0)
        {
            return new ResponseResult(CodeMsg.ROLE_HASUSED);
        }

        //删除角色信息
        roleMapper.deleteBatchIds(integers);

        //删除权限信息
        LambdaQueryWrapper<RolePermission> rolePermissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        rolePermissionLambdaQueryWrapper.in(RolePermission::getRid,integers);
        rolePermissionMapper.delete(rolePermissionLambdaQueryWrapper);
        return new ResponseResult(CodeMsg.SUCCESS);
    }

    @Override
    public ResponseResult updateRole(Role role) {
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleLambdaQueryWrapper.eq(Role::getName,role.getName());
        Role role1 = roleMapper.selectOne(roleLambdaQueryWrapper);
        if (ObjectUtils.isNotEmpty(role1)&&role.getId()!=role1.getId())
        {
            return new ResponseResult(CodeMsg.ROLE_EXIST);
        }
        roleMapper.updateById(role);

        return new ResponseResult(CodeMsg.SUCCESS);
    }

    @Override
    public ResponseResult selectPermissionByRoleId(Integer id) {

        LambdaQueryWrapper<RolePermission> rolePermissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        rolePermissionLambdaQueryWrapper.in(RolePermission::getRid,id);
        List<RolePermission> rolePermissions = rolePermissionMapper.selectList(rolePermissionLambdaQueryWrapper);

        List<Integer> collect = rolePermissions.stream().map(rolePermission -> {
            return rolePermission.getPid();
        }).collect(Collectors.toList());

        return new ResponseResult(collect,null,CodeMsg.SUCCESS);
    }
}




